package com.example.smartcity.Service.Impl;

import com.alibaba.fastjson.JSONObject;
import com.example.smartcity.Entity.Collect;
import com.example.smartcity.Entity.News;
import com.example.smartcity.Entity.login;
import com.example.smartcity.Mappers.CollectionMapper;
import com.example.smartcity.Mappers.NewsCommentMapper;
import com.example.smartcity.Mappers.NewsMapper;
import com.example.smartcity.Service.NewsCommentService;
import com.example.smartcity.Service.NewsService;
import com.example.smartcity.utils.MybatisUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("NewsService")
public class NewsServiceImpl implements NewsService {

    public String findNewsByTitleOrContent(String words){
        String words2 = words;
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        NewsMapper newsMapper =sqlSession.getMapper(NewsMapper.class);
        List<News> news = newsMapper.findByTitleOrContent(words,words2);
        sqlSession.close();
        JSONObject map = new JSONObject();
        map.put("total",news.size());
        map.put("rows",news);
        map.put("code",200);
        map.put("msg","查询成功");
        System.out.println(JSONObject.toJSONString(map));
        return JSONObject.toJSONString(map);
    }
    public void UpdateViewsNumber(int id){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        NewsMapper newsMapper =sqlSession.getMapper(NewsMapper.class);
        newsMapper.updateViewsNumber(id);
        sqlSession.commit();
        sqlSession.close();
        System.out.println("更新"+id+"号新闻的浏览量成功");
    }
    public void UpdateLikesNumber(int id){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        NewsMapper newsMapper =sqlSession.getMapper(NewsMapper.class);
        newsMapper.updateLikesNumber(id);
        sqlSession.commit();
        sqlSession.close();
        System.out.println("更新"+id+"号新闻的点赞量成功");
    }
    public String findNewsByCategory(String category){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        NewsMapper newsMapper =sqlSession.getMapper(NewsMapper.class);
        List<News> news = newsMapper.findNewsByCategory(category);
        sqlSession.close();
        Gson gson = new Gson();
        String jsonString = gson.toJson(news);
        System.out.println(jsonString);
        JSONObject map = new JSONObject();
        map.put("total",news.size());
        map.put("rows",news);
        map.put("code",200);
        map.put("msg","查询成功");
        return JSONObject.toJSONString(map);
    }
    public String findAll(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        NewsMapper newsMapper =sqlSession.getMapper(NewsMapper.class);
        List<News> news = newsMapper.findAll();
        sqlSession.close();
        Gson gson = new Gson();
        String jsonString = gson.toJson(news);
        System.out.println(jsonString);
        JSONObject map = new JSONObject();
        map.put("total",news.size());
        map.put("rows",news);
        map.put("code",200);
        map.put("msg","查询成功");
        System.out.println(JSONObject.toJSONString(map));
//        try{
//            List<News> news2 = map.getJSONArray("rows").toJavaList(News.class);
//            for (News news3 : news2) {
//                System.out.println(news3.getTitle());
//                // 对news对象进行操作
//            }
//        }catch (Exception e){
//            System.out.println(e);
//        }

        return JSONObject.toJSONString(map);
    }
    public int iscollect(int userid,String newstitle){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        NewsCommentMapper newsCommentMapper = sqlSession.getMapper(NewsCommentMapper.class);
        int newsid = newsCommentMapper.getTextidByTextName(newstitle);
        System.out.println(newsid);
        System.out.println(userid);
        CollectionMapper collectionMapper = sqlSession.getMapper(CollectionMapper.class);

        Collect iscollect = collectionMapper.isCollection(userid,newsid);
        System.out.println(iscollect);
        sqlSession.close();
        if(iscollect==null){return 0;}else{;return 1;}
    }
    public void cancelcollect(int userid,String newstitle){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        NewsCommentMapper newsCommentMapper = sqlSession.getMapper(NewsCommentMapper.class);
        int newsid = newsCommentMapper.getTextidByTextName(newstitle);
        CollectionMapper collectionMapper = sqlSession.getMapper(CollectionMapper.class);
        collectionMapper.deleteCollection(userid,newsid);
        sqlSession.commit();
        sqlSession.close();
    }
    public void addcollect(int userid,String newstitle){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        NewsCommentMapper newsCommentMapper = sqlSession.getMapper(NewsCommentMapper.class);
        int newsid = newsCommentMapper.getTextidByTextName(newstitle);
        CollectionMapper collectionMapper = sqlSession.getMapper(CollectionMapper.class);
        collectionMapper.addToCollection(userid,newsid);
        sqlSession.commit();
        sqlSession.close();
    }
    public String getCollection(int userid){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        CollectionMapper collectionMapper = sqlSession.getMapper(CollectionMapper.class);
        List<Integer> collectionlist = collectionMapper.ArticleIdList(userid);
        NewsMapper newsMapper = sqlSession.getMapper(NewsMapper.class);
        List<News> newsList = new ArrayList<>();
        for(int item:collectionlist){
            News news = newsMapper.getNewsById(item);
            newsList.add(news);
        }
        JSONObject map = new JSONObject();
        map.put("total",newsList.size());
        map.put("rows",newsList);
        map.put("code",200);
        map.put("msg","查询成功");
        return map.toJSONString();
    }
    public void addViewNumber(int newstitle){
        SqlSession sqlSession =  MybatisUtils.getSqlSession();
        NewsMapper newsMapper = sqlSession.getMapper(NewsMapper.class);
        newsMapper.updateViewsNumber(newstitle);
    }


}
